package ldh.serialize.type;

import java.io.IOException;
import java.sql.Date;

import ldh.serialize.BinarySerialized;
import ldh.serialize.io.ByteBuf;
import ldh.serialize.util.SerializedUtil;

public class Date2Serialized implements BinarySerialized<Date> {

	@Override
	public void write(ByteBuf out, Date date) throws IOException {
		Long time = null;
		if (date != null) {
			time = date.getTime();
		}
		SerializedUtil.writeLong(out, time);
	}

	@Override
	public Date read(ByteBuf in) throws IOException {
		Long time = SerializedUtil.readLong(in);
		if (time == null) {
			return null;
		}
		return new Date(time);
	}

	@Override
	public Class<Date> getClassType() {
		return Date.class;
	}
}
